<?php

namespace App\Models\Factory;

use App\Helpers\DateUtils;
use App\Helpers\Generator\TokenGenerator;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\User;
use App\Models\Orm\UserBanks;
use App\Models\Orm\UserCertify;
use App\Models\Orm\UserIdentity;
use App\Models\Orm\UserInvite;
use App\Models\Orm\UserInviteCode;
use App\Models\Orm\UserProfile;
use App\Strategies\SexStrategy;

/**
 * 用户工厂
 * Class UserFactory
 * @package App\Models\Factory
 */
class UserFactory extends AbsModelFactory
{

    /**
     * 通过手机号查询用户信息
     */
    public static function getUserByMobile($mobile)
    {
        $user = User::where(['userphone' => $mobile])->first();
        return $user?$user : false;
    }
    /**
     * 创建
     * @param $mobile
     */
    public static function createUser($mobile,$terminal,$app_name)
    {
        $user = User::firstOrCreate(
            [
                'userphone' => $mobile
            ],
            [
                'userphone' => $mobile,
                'terminal' => $terminal,
                'app_name' => $app_name,
                'registe_time' => DateUtils::getToday(),
                'created_ip'      => Utils::ipAddress(),
            ]
        );
        return $user? true : false;
    }

    /**
     * 生成的token保存进用户表
     */
    public static function addUserTokenByMobile($mobile,$data)
    {
        $user = User::where(['userphone' => $mobile])->update($data);
        return $user;
    }


    /*
     * 通过token获取到用户id
     */

    public static function getIdByToken($token)
    {
        $user = User::where(['token' => $token])->first();
        return $user ? $user->id : '';
    }

    /**
     * 通过token查找用户对象
     *
     * @param $token
     * @return mixed
     */
    public static function getUserByToken($token)
    {
        return User::where('token', $token)->first();
    }

    public static function getUserByTokenId($token)
    {
        $user = User::where('token' , $token)->first();
        return  $user ? $user -> id : '';

    }


}